package com.qfedu.springmvc02.controller;


import com.qfedu.springmvc02.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin("http://localhost:8080")//跨域请求
public class UserController {


    //查全部
    @GetMapping("/users")
    public List<User> showAll(){
        List<User> users = new ArrayList<>();
        users.add(new User(1,"张三","1234"));
        users.add(new User(2,"李四","12345"));
        users.add(new User(3,"王二","123456"));
        return users;
    }

    //查单个
    @GetMapping("/users/{id}")
    public User selectOne(@PathVariable("id")Integer id){
        return User.builder().id(1).name("张三").password("1234").build();
    }

    //add添加
    @PostMapping("/users")
    public String add(@RequestBody User user){
        System.out.println("新增" + user);
        return "{code:1,success:true}";
    }

    //修改
    @PutMapping("/users")
    public String update(@RequestBody User user){
        System.out.printf("修改%s",user);
        return "{code:1,success:true}";
    }

    //删除
    @DeleteMapping("/users/{id}")
    public String delete(@PathVariable("id") Integer id){
        System.out.printf("删除为%d的用户",id);
        return "{code:1,success:true}";
    }

}
